package cn.cuiliangpeng.user.service.impl;

import cn.cuiliangpeng.basic.consts.RedisConstants;
import cn.cuiliangpeng.basic.domain.dto.RegisterDto;
import cn.cuiliangpeng.basic.exception.BusinessException;
import cn.cuiliangpeng.basic.exception.ErrorCode;
import cn.cuiliangpeng.basic.utils.AjaxResult;
import cn.cuiliangpeng.basic.utils.MD5Utils;
import cn.cuiliangpeng.basic.utils.StrUtils;
import cn.cuiliangpeng.user.domain.Logininfo;
import cn.cuiliangpeng.user.domain.User;
import cn.cuiliangpeng.user.mapper.LogininfoMapper;
import cn.cuiliangpeng.user.mapper.UserMapper;
import cn.cuiliangpeng.user.service.IUserService;
import cn.cuiliangpeng.basic.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 业务实现类：
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 手机号码注册
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public AjaxResult phoneRegister(RegisterDto dto) {
     String phone= dto.getPhone();

     String password =dto.getPassword();
        if(StringUtils.isEmpty(phone) || StringUtils.isEmpty(dto.getPhoneCode()) ||
        StringUtils.isEmpty(password) || StringUtils.isEmpty(dto.getPasswordRepeat()) ){
            throw new BusinessException("信息不能为空!!!");
        }
        //1.校验密码是否一致
        if (!dto.getPassword().equals(dto.getPasswordRepeat())){
            throw new BusinessException(ErrorCode.PASS_WORD_ERROR.getCode());
        }
        //2.校验用户是否已注册
        User user1=userMapper.findByPhone(dto.getPhone());
        if (user1!=null){
            throw new BusinessException(ErrorCode.USER_ERROR.getCode());
        }
        //3.重redis中查询手机验证码
        String phoneCode = (String)redisTemplate.opsForValue().get(dto.getPhone() + RedisConstants.REGISTER_KEY);
        System.out.println(dto.getPhoneCode()+phoneCode);
        //4.手机验证码为空  过期了抛异常
        if (phoneCode==null){
            throw new BusinessException(ErrorCode.IMAGE_CODE_TIME_OUT.getCode());
        }
        //5.有手机验证码但是输入错误 二次不一样抛异常
        if(!dto.getPhoneCode().equals(phoneCode.split(":")[0])) {
            throw new BusinessException(ErrorCode.IMAGE_CODE_ERROR.getCode());
        }
        //6.添加到数据库
        User user= map2User(phone,password);
        Logininfo logininfo =user2Logininfo(user);
        logininfoMapper.save(logininfo);
        user.setLogininfoId(logininfo.getId());
        userMapper.save(user);
        return AjaxResult.me();
    }

    private Logininfo user2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setType(1);//1表示用户
        logininfo.setDisable(true);
        return logininfo;
    }
    private User map2User(String phone, String password) {
        User user = new User();
        user.setUsername(phone);
        user.setPhone(phone);
        //对密码和盐值进行处理
        String salt  = StrUtils.getComplexRandomString(32);
        String byMd5 = MD5Utils.encrypByMd5(salt+password);
        user.setSalt(salt);
        user.setPassword(byMd5);
        user.setState(1);
        return user;
    }
}
